package com.zbkj.front.controller.mszs;

import com.zbkj.common.request.mszs.MszsMeituanLoginRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.service.service.sms.SmsService;
import com.zbkj.service.thirdparty.login.LoginFrontService;
import com.zbkj.service.thirdparty.login.request.*;
import com.zbkj.service.thirdparty.login.response.LoginResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 宿助手-商家用户-登录注册（前端-controller控制类）、
 * @author lijie
 */
@Slf4j
@RestController()
@RequestMapping("api/front/mszs")
@Api(tags = "民宿助手-商家用户-登录注册 ")
public class MszsLoginFrontController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private LoginFrontService loginService;

    @ApiOperation(value = "发送-登录验证码短信")
    @RequestMapping(value = "/sendCode", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="phone", value="手机号码", required = true)
    })
    public CommonResult<Object> sendCode(@RequestParam String phone){
        if(smsService.sendCommonCode(phone)){
            return CommonResult.success("发送成功");
        }else{
            return CommonResult.failed("发送失败");
        }
    }

    @ApiOperation(value = "第三方登录-美团商家入驻(账号密码和手机号验证码注册登录)")
    @RequestMapping(value = "/loginMeituan", method = RequestMethod.POST)
    public CommonResult<LoginResponse> login(@RequestBody @Validated MszsMeituanLoginRequest loginRequest) {
        return CommonResult.success(loginService.loginMeituan(loginRequest));
    }

    @ApiOperation(value = "第三方登录-微信小程序授权登录(不需要强制绑定手机号)")
    @RequestMapping(value = "/loginWeixinXcxNotPhone", method = RequestMethod.POST)
    public CommonResult<LoginResponse> loginWeixinXcxNotPhone(@RequestBody @Validated WeixinXcxLoginRequest request){
        return CommonResult.success(loginService.loginWeixinXcxNotPhone(request));
    }

    @ApiOperation(value = "第三方登录-微信小程序授权登录(强制绑定手机号)")
    @RequestMapping(value = "/loginWeiXinXcx", method = RequestMethod.POST)
    public CommonResult<LoginResponse> loginWeiXinXcx(@RequestBody @Validated WeixinXcxLoginRequest request){
        return CommonResult.success(loginService.loginWeixinXcx(request));
    }

    @ApiOperation(value = "绑定手机号注册并登录-(获取微信用户绑定的手机号)")
    @RequestMapping(value = "/bindPhoneWeixin", method = RequestMethod.POST)
    public CommonResult<LoginResponse> bindPhoneWeixin(@RequestBody @Validated BindPhoneRegisterAndLoginWeiXinRequest request){
        return CommonResult.success(loginService.bindPhoneWeixin(request));
    }

    @ApiOperation(value = "绑定手机号注册并登录-(普通)")
    @RequestMapping(value = "/bindPhone", method = RequestMethod.POST)
    public CommonResult<LoginResponse> bindPhone(@RequestBody @Validated BindPhoneRegisterAndLoginRequest request){
        return CommonResult.success(loginService.bindPhone(request));
    }

    @ApiOperation(value = "手机号验证码登录")
    @RequestMapping(value = "/loginMobileCode", method = RequestMethod.POST)
    public CommonResult<LoginResponse> loginPhoen(@RequestBody @Validated LoginMobileRequest loginRequest) {
        return CommonResult.success(loginService.loginPhoen(loginRequest));
    }

    @ApiOperation(value = "账号密码登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public CommonResult<LoginResponse> login(@RequestBody @Validated AccountPwdLoginRequest loginRequest) {
        return CommonResult.success(loginService.loginAccountPwd(loginRequest));
    }

    @ApiOperation(value = "退出登录")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public CommonResult<String> loginOut(HttpServletRequest request){
        loginService.loginOut(request);
        return CommonResult.success();
    }
}
